# Copyright: (C) 2010 RobotCub Consortium
# Author: Lorenzo Natale
# CopyPolicy: Released under the terms of the GNU GPL v2.0.

if (NOT ICUB_FOUND)
  message(STATUS "Using iCub from install")

  set(ICUB_VERSION @ICUB_VERSION@)
  # If YARP_LIBRARIES has been valorized by a previous call of find_package(YARP)
  # in user CMakelists.txt save it in temporary variable to restore it later.
  if(DEFINED YARP_LIBRARIES)
    set(_temp_yarp_libs ${YARP_LIBRARIES})
  endif()

  include(CMakeFindDependencyMacro)
  find_dependency(YCM @YCM_REQUIRED_VERSION@ REQUIRED)
  find_dependency(YARP @YARP_REQUIRED_VERSION@ REQUIRED COMPONENTS os conf sig dev math gsl)

  #set(ICUB_INCLUDE_DIRS "@ICUB_INCLUDE_DIRS@" CACHE INTERNAL "Include directories needed for iCub")
  set(ICUB_MODULE_PATH "@ICUB_MODULE_PATH@" CACHE INTERNAL "iCub cmake scripts directory")
  set(ICUB_LIBRARIES "@ICUB_TARGETS@" CACHE INTERNAL "List of iCub libraries")
  set(ICUB_LINK_FLAGS "@ICUB_LINK_FLAGS@" CACHE INTERNAL "List of iCub linker options")
  set(ICUB_LINK_DIRECTORIES "@ICUB_LINK_DIRECTORIES@")
  set(ICUB_INSTALL_PREFIX "@CMAKE_INSTALL_PREFIX@")

  set(ICUB_PLUGIN_MANIFESTS_INSTALL_DIR "@ICUB_PLUGIN_MANIFESTS_INSTALL_DIR@")
  set(ICUB_MODULES_INSTALL_DIR "@ICUB_MODULES_INSTALL_DIR@")
  set(ICUB_APPLICATIONS_INSTALL_DIR "@ICUB_APPLICATIONS_INSTALL_DIR@")
  set(ICUB_TEMPLATES_INSTALL_DIR "@ICUB_TEMPLATES_INSTALL_DIR@")
  set(ICUB_APPLICATIONS_TEMPLATES_INSTALL_DIR "@ICUB_APPLICATIONS_TEMPLATES_INSTALL_DIR@")
  set(ICUB_MODULES_TEMPLATES_INSTALL_DIR "@ICUB_MODULES_TEMPLATES_INSTALL_DIR@")
  set(ICUB_CONTEXTS_INSTALL_DIR "@ICUB_CONTEXTS_INSTALL_DIR@")

  # Remove the old file if it exists. This is a temporary fix
  if(EXISTS "@CMAKE_INSTALL_PREFIX@/lib/ICUB/icub-export-install-includes.cmake")
    file(REMOVE "@CMAKE_INSTALL_PREFIX@/lib/ICUB/icub-export-install-includes.cmake")
  endif()

  include("@CMAKE_INSTALL_PREFIX@/lib/ICUB/icub-export-install.cmake")
  include("@CMAKE_INSTALL_PREFIX@/lib/ICUB/icub-export-inst-includes.cmake")

  # This is not ideal and should be removed. At the moment
  # only needed by OpenCV
  link_directories(${ICUB_LINK_DIRECTORIES})

  set (ICUB_FOUND TRUE)
  if(DEFINED _temp_yarp_libs)
    list(APPEND YARP_LIBRARIES ${_temp_yarp_libs})
    list(REMOVE_DUPLICATES YARP_LIBRARIES)
  endif()
endif (NOT ICUB_FOUND)
